//
//  CompanyNoticeListViewController.m
//  Thinkape-iOS
//
//  Created by tixa on 15/4/24.
//  Copyright (c) 2015年 TIXA. All rights reserved.
//

#import "CompanyNoticeListViewController.h"
#import "NoticeListViewCell.h"
#import "NewsModel.h"
#import "NewsDetailViewController.h"
#import "GonggaoMod.h"
#import "PFAdView.h"
#import "YLCarouselView.h"
@interface CompanyNoticeListViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *topView;

@property (nonatomic,assign)NSInteger number;

//页数
@property(nonatomic,assign)int pagin;



@property (nonatomic,strong)YLCarouselView *carousel;



@property(nonatomic,assign)NSInteger numbers;

@end

@implementation CompanyNoticeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

      self.dataArray =[NSMutableArray array];

    _number=0;

    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshNewsListData];
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{


        if (_number!=0&&self.dataArray.count>=_number) {
             [self.tableView.footer endRefreshing];
        }else
        {
        [self loadViewIfNeededs];

//        if (_number != 0 && self.dataArray.count >= _numbers) {
//            
//            [self.tableView.footer endRefreshing];
//        }else{
//            [self loadViewIfNeededs];
//
//        }

        }
        }];

    
    
    
    [self.tableView.header beginRefreshing];
    
    //轮播图：
    [self configCarousel];
    // Do any additional setup after loading the view.
}

- (void)configCarousel
{
    self.carousel = [YLCarouselView initWithFrame:CGRectMake(12, 76, self.view.frame.size.width-24 , (self.view.frame.size.width-24)*(181.0/351.0)) hasTimer:YES interval:2 placeHolder:[UIImage imageNamed:@"loading"]];
    [self.view addSubview:self.carousel];
    
    NSArray *array = @[@"img1",@"img3",@"img2"];
    [self.carousel setupWithLocalImgArray:array];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - HttpRequest

//- (void)refreshNewsListData{
//    //http://27.115.23.126:3032/ashx/mobilenew.ashx?ac=GetInfoList&u=1&ukey=abc&pi=0&ps=20
//    [RequestCenter GetRequest:[NSString stringWithFormat:@"ac=GetInfoList&u=%@&ukey=%@&pi=0&ps=20",self.uid,self.ukey]
//                   parameters:nil
//                      success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
//                          NSArray *dataArr = [[responseObject objectForKey:@"msg"] objectForKey:@"data"];
//                          [self.dataArray removeAllObjects];
//                          for (NSDictionary *dic in dataArr) {
//                              NewsModel *model = [[NewsModel alloc] init];
//                              [model setValuesForKeysWithDictionary:dic];
//                              if ([model.isannounce boolValue]) {
//                                  [self.dataArray addObject:model];
//                              }
//                          }
//                          [self.tableView.header endRefreshing];
//                          [self.tableView reloadData];
//                      }
//                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//                          [self.tableView.header endRefreshing];
//                      }
//            showLoadingStatus:NO];
//}
-(void)refreshNewsListData
{
    _pagin=0;
    NSString *str =[NSString stringWithFormat:@"ac=GetInfoList&u=%@&pi=%d&ps=20",self.uid,_pagin];
    [RequestCenter GetRequest:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(NSURLSessionDataTask *operation, NSDictionary *responseObject) {
        
        NSString *error =[responseObject objectForKey:@"error"];
        int handan = [error intValue];
        if (handan!=0) {
               [SVProgressHUD showErrorWithStatus:@"请求失败"];
            return;
        }
        
        NSDictionary *msg =[responseObject objectForKey:@"msg"];

       
        NSString *page = [msg objectForKey:@"total"];
        //页数

        _number = (NSInteger)[page integerValue];

      
        //页数

        

        if ([msg objectForKey:@"data"]) {
            NSArray *gongGao =[msg objectForKey:@"data"];
            
//            self.dataArray =[NSMutableArray array];

//        self.pagin = [page intValue];
       
        if ([msg objectForKey:@"data"]) {
            NSArray *gongGao =[msg objectForKey:@"data"];
            self.dataArray =[NSMutableArray array];

            for (NSDictionary *mesegi in gongGao) {
                GonggaoMod *model =[[GonggaoMod alloc] init];
                [model setValuesForKeysWithDictionary:mesegi];
                [self.dataArray addObject:model];
            }
        }

        [self.tableView .header endRefreshing];
      
         [self.tableView reloadData];
        
        
        

        }
      

    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [self.tableView.header endRefreshing];
        
    } showLoadingStatus:NO];
    
}
-(void)loadViewIfNeededs{
    
    _pagin +=1;
    NSString *str =[NSString stringWithFormat:@"ac=GetInfoList&u=%@&pi=%d&ps=20",self.uid,_pagin];
    [RequestCenter GetRequest:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(NSURLSessionDataTask *operation, NSDictionary *responseObject) {
        
        NSString *error =[responseObject objectForKey:@"error"];
        int handan = [error intValue];
        if (handan!=0) {
            [SVProgressHUD showErrorWithStatus:@"请求失败"];
            return;
        }
        NSDictionary *msg =[responseObject objectForKey:@"msg"];
        NSString *page = [responseObject objectForKey:@"total"];
        _number = (NSInteger)[page integerValue];
        


        

        _number = [page integerValue];

        _numbers = [page integerValue];

        
        if ([msg objectForKey:@"data"]) {
            NSArray *gongGao =[msg objectForKey:@"data"];
            

            for (NSDictionary *mesegi in gongGao) {
                
                GonggaoMod *model =[[GonggaoMod alloc] init];
                [model setValuesForKeysWithDictionary:mesegi];
                
                [self.dataArray addObject:model];
            }
        }
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [self.tableView.footer endRefreshing];
        
    } showLoadingStatus:NO];
}
#pragma mark - UITableView Delegate && DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellid = @"cell";
    NoticeListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

    GonggaoMod *model = [self.dataArray safeObjectAtIndex:indexPath.row];
    cell.titleLabel.text = model.title;
    cell.descLabel.text = model.title;
    cell.timeLabel.text = model.create_date;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GonggaoMod *model = [self.dataArray safeObjectAtIndex:indexPath.row];
    NewsDetailViewController *vc = [[NewsDetailViewController alloc] init];
    vc.urlStr = model.msgurl;
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
